<template>
	<view class="container">
		<view class="pic-list">
			<view @tap="picPreview(picIndex)" class="pic-item" v-for="(pic,picIndex) in uploadPicList" :key="picIndex">
				<image :src="pic"></image>
			</view>
			<view class="pic-item" @tap="selectPic">
				<image src="../../static/images/publish.png"></image>
			</view>
		</view>
		<view class="save-box">
			<view class="save-btn" @tap="save">保 存</view>
		</view>
	</view>
</template>

<script>
	import { http, showToastMsg, domain } from '../../utils'
	
	export default {
		data() {
			return {
				domain: domain,
				url: domain + '/api/upload',
				uploadPicList: [],
				formData: {
					type: 3,
					pictures: []
				},
				userInfo: null
			}
		},
		
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
			for (let i = 0; i < this.userInfo.userAlbums.length; i++) {
				if (this.userInfo.userAlbums[i].length > 0) {
					this.formData.pictures.push(this.userInfo.userAlbums[i])
					this.uploadPicList.push(this.domain + this.userInfo.userAlbums[i])
				}
			}
		},
		
		methods: {
			
			selectPic() {
				const that = this
				if (that.uploadPicList.length >= 20) {
					return uni.showToast({
					    title: '最多只能上传20张图片',
					    icon: 'none'
					})
				}
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths
						const totalLength = tempFilePaths.length + that.uploadPicList.length
						if (totalLength > 20) {
							return uni.showToast({
							    title: '最多只能上传20张图片',
							    icon: 'none'
							})
						}
						uni.showToast({
							title: '开始上传',
							icon: 'none'
						})
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: that.url,
								filePath: tempFilePaths[i],
								name: 'file',
								formData: {fileType: 'images'},
								success: (uploadFileRes) => {
									const res = JSON.parse(uploadFileRes.data)
									that.uploadPicList.push(that.domain + res.data.url)
									that.formData.pictures.push(res.data.url)
								},
								fail: (err) => {
									uni.showToast({
										title: err.errMsg,
										icon: 'none'
									})
									console.log(err)
								}
							})
						}
					}
				})
			},
			
			picPreview(i) {
				const that = this
				uni.previewImage({
					current: i,
					indicator: 'none',
					urls: that.uploadPicList
				})
			},
			
			save() {
				const that = this
				if (that.formData.pictures.length <= 0) {
					return showToastMsg('请至少上传一张图片')
				}
				http('/user/modify', that.formData).then(res => {
					if (res.code == 20000) {
						http('/no-login/islogin').then(isLoginRes => {
							uni.setStorageSync('userInfo', isLoginRes.data.userInfo)
							showToastMsg(res.message)
							uni.redirectTo({
								url: '/pages/teacher_zone_index/teacher_zone_index'
							})
						})
					}
				})
			}
			
		}
	}
</script>

<style>
	.container {
		width: 750rpx;
		height: auto;
	}
	
	.pic-list {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-left: 26rpx;
		margin-top: 30rpx;
	}
	
	.pic-item {
		width: 236rpx;
	}
	
	.pic-item image {
		width: 226rpx;
		height: 226rpx;
		border-radius: 30rpx;
	}
	
	.save-box {
		margin-top: 40rpx;
		width: 750rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
	}
	
	.save-btn {
		width: 633rpx;
		height: 80rpx;
		background-image: linear-gradient(90deg, #ff8604 0%, #fbb02c 100%), linear-gradient(#ffffff, #ffffff);
		border-radius: 44rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		color: #ffffff;
	}
</style>
